package org.datascooter.impl;

import org.datascooter.bundle.DSMapper;
import org.datascooter.bundle.EntityBundle;
import org.datascooter.bundle.attribute.PersistAttribute;
import org.datascooter.exception.DataScooterException;
import org.datascooter.utils.LangUtils;
import org.datascooter.utils.policy.Function;
import org.datascooter.utils.policy.SnipType;

public final class DataSnipFactory {

	private DataSnipFactory() {
	}

	public static DataSnip select(Object entity) {
		return new DataSnip(LangUtils.entityName(entity), SnipType.SELECT);
	}

	public static DataSnip select(Object entity, int fromRow, int fetchSize) {
		return new DataSnip(LangUtils.entityName(entity), SnipType.SELECT, fromRow, fetchSize);
	}

	public static DataSnip count(Object entity) {
		DataSnip snip = new DataSnip(LangUtils.entityName(entity), SnipType.FUNCTION);
		snip.setFunction(Function.COUNT);
		return snip;
	}

	public static DataSnip delete(Object entity) {
		return new DataSnip(LangUtils.entityName(entity), SnipType.DELETE);
	}

	public static DataSnip none(Object entity) {
		return new DataSnip(LangUtils.entityName(entity), SnipType.NONE);
	}

	public static DataSnip drop(Object entity) {
		return new DataSnip(LangUtils.entityName(entity), SnipType.DROP);
	}

	public static DataSnip insert(Object entity) {
		return new DataSnip(LangUtils.entityName(entity), SnipType.INSERT);
	}

	public static DataSnip create(Object entity) {
		return new DataSnip(LangUtils.entityName(entity), SnipType.CREATE);
	}

	public static DataSnip update(Object obj) {
		if (!(obj instanceof String) && !(obj instanceof Class<?>)) {
			try {
				EntityBundle bundle2 = DSMapper.getBundle(obj);
				PersistAttribute id2 = bundle2.id;
				return new DataSnip(bundle2.entity, SnipType.UPDATE).where(id2.name, id2.getValue(obj));
			} catch (Exception e) {
				throw new DataScooterException(e);
			}
		}
		return new DataSnip(LangUtils.entityName(obj), SnipType.UPDATE);
	}

	public static DataSnip group() {
		return new DataSnip(null, SnipType.GROUP);
	}
}
